// pages/withdraw-deposit/withdraw-deposit.js
const SERVICE = require("../../service.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    Money:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this;
    let openid = wx.getStorageSync("token");
    wx.showNavigationBarLoading();
    SERVICE.getWithdrawDepositInfo({ openid }, (stauts, data) => {
      wx.hideNavigationBarLoading();
      console.log(data);
      _this.setData({
        withdrawDeposiInfo: data.data
      });
    });

    SERVICE.WithdrawalRecord({ openid }, (stauts, data) => {
      console.log(data);
      _this.setData({
        RecordList: data.data
      });
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  withdrawDeposit(e){ 
    let _this = this,money = parseFloat(e.currentTarget.dataset.money),bankcardid = e.currentTarget.dataset.bankcardid;
     if (money == 0){
      wx.showToast({
        title: "已达到您的余额上限",
        icon:"none",
        mask:true
      });
    } else if (bankcardid === null){
      wx.showToast({
        title: "请选择或添加银行卡",
        icon: "none",
        mask: true
      });
    }else{
      SERVICE.withdrawDeposit({ amount: money, bank_card_id: bankcardid},(status,data)=>{
        if (status) {
          _this.onShow();
          wx.showToast({
            title: "提现成功",
            icon: "none",
            mask: true
          });
        } else {
          wx.showToast({
            title: data.message,
            icon: "none",
            mask: true
          });
        }
      });
    }
  },
  withdrawDepositInput(e){
    let value = parseFloat(e.detail.value),money = parseFloat(e.currentTarget.dataset.money);
      this.setData({
        Money:value
      });
    if (value > money){
      wx.showToast({
        title: "已超过您的余额上限",
        icon: "none",
        mask: true
      });
    }
  },
  withdrawDeposit2(e){
    let _this = this,td = this.data,
       getmoney = parseFloat(e.currentTarget.dataset.money),
      bankcardid = e.currentTarget.dataset.bankcardid;
    let openid = wx.getStorageSync("token");    
      
    if (getmoney > 0){
      if (td.Money == 0) {
        wx.showToast({
          title: "请输入提现金额",
          icon: "none",
          mask: true
        });
      } else if (td.Money > getmoney){
        wx.showToast({
          title: "已超过您的余额上限",
          icon: "none",
          mask: true
        });
      } else if (bankcardid === null){
        wx.showToast({
          title: "请选择或添加银行卡",
          icon: "none",
          mask: true
        });
      }else{
        SERVICE.withdrawDeposit({ amount: td.Money, bank_card_id: bankcardid,openid }, (status, data) => {
          console.log(data);
          if(status){
            _this.onShow();
            wx.showToast({
              title: "提现成功",
              icon: "none",
              mask: true
            });
          }else{
            wx.showToast({
              title: data.message,
              icon: "none",
              mask: true
            });
          }
        });
      }
    }else{
      wx.showToast({
        title: "已达到您的余额上限",
        icon: "none",
        mask: true
      });
    }
  },
  add(){
    wx.navigateTo({
      url: "../bank-card/bank-card",
    })
  }
})